package com.timoon.webcomponent.utility.product.searchresult;

import java.util.List;
import java.util.Map;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.sample.dao.model.Product;
import com.timoon.sample.page.SingleProductPage;
import com.timoon.webcomponent.utility.product.girdpic.GridBigPicPanel;
import com.timoon.webcomponent.utility.product.girdpic.GridBigPicService;

/**
 *<p>搜索界面产品,grid显示面板，可用type和名字模糊匹配两方面搜索</p>
 *<p>实质是拓展GridBigPicPanel,添加功能按钮,实现分页功能</p>
 *<p>必须传入的pageParameters有product_sort_id或者searchInfo</p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class SearchResultPanel extends Panel {
	
	@SpringBean(name="com.timoon.sample.service.GridBigPicServiceImpl")
	private GridBigPicService gridBigPicService;
	
	private Class<? extends WebPage> parentPage;
	
	protected PageParameters pageParameters;
	
	private Integer productSortId;//当前分类id
	
	private Integer currentPage=1;//当前page
	
	private Integer rows=2;//默认分页大小
	
	private Integer skip=0;//页面跳转跨越页码，2代表末页,-2代表首页
	
	private String searchInfo="default";
	
	private boolean firstBtnEnable=true;
	private boolean lastBtnEnable=true;
	private boolean nextBtnEnable=true;
	private boolean finalBtnEnabel=true;

	/**
	 * 
	 * @param id
	 * @param productSortId
	 * @param parentPage
	 */
	public SearchResultPanel(String id,PageParameters pageParameters,Class<? extends WebPage> parentPage) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.pageParameters=pageParameters;		
		
		this.parentPage=parentPage;
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		
		//此方法必须在renderForm()之前，因为此方法中判断了renderForm()中button是否可用
		renderPicGrid();
		
		renderForm();
		
	}

	private void renderForm() {
		// TODO Auto-generated method stub
		Form form=new Form("wk_form");
		add(form);
		
		renderBtn(form);
	}

	private void renderBtn(Form form) {
		// TODO Auto-generated method stub
		
		//首页
		form.add(new AjaxButton("wk_first"){
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				PageParameters parameters=new PageParameters();
				
				parameters.set("product_sort_id", productSortId);//当前分类
				parameters.set("searchInfo", searchInfo);//搜索信息
				parameters.set("skip", "-2");//跳转至首页
				parameters.set("currentPage", currentPage);//将当前页码传递
				parameters.set("rows", rows);
				
				this.setResponsePage(parentPage,parameters);
			}
			{
				if(!firstBtnEnable){
					this.setEnabled(false);
				}
			}
		});
		
		form.add(new AjaxButton("wk_final") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				PageParameters parameters=new PageParameters();
				
				parameters.set("product_sort_id", productSortId);//当前分类
				parameters.set("searchInfo", searchInfo);//搜索信息
				parameters.set("skip", "2");//末页
				parameters.set("currentPage", currentPage);//将当前页码传递
				parameters.set("rows", rows);
				
				this.setResponsePage(parentPage,parameters);
			}
			{
				if(!finalBtnEnabel){
					this.setEnabled(false);
				}
			}
		});
		
		form.add(new AjaxButton("wk_next") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				PageParameters parameters=new PageParameters();
				
				parameters.set("product_sort_id", productSortId);//当前分类
				parameters.set("searchInfo", searchInfo);//搜索信息
				parameters.set("skip", "1");//下一页
				parameters.set("currentPage", currentPage);//将当前页码传递
				parameters.set("rows", rows);
				
				this.setResponsePage(parentPage,parameters);
			}
			{
				if(!nextBtnEnable){
					this.setEnabled(false);
				}
			}
		});
		
		form.add(new AjaxButton("wk_last") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				PageParameters parameters=new PageParameters();
				
				parameters.set("product_sort_id", productSortId);//当前分类
				parameters.set("searchInfo", searchInfo);//搜索信息
				parameters.set("skip", "-1");//上一页
				parameters.set("currentPage", currentPage);//将当前页码传递
				parameters.set("rows", rows);
				
				this.setResponsePage(parentPage,parameters);
			}
			
			{
				if(!lastBtnEnable){
					this.setEnabled(false);
				}
			}
		});
		

	}

	private void renderPicGrid() {
		// TODO Auto-generated method stub
		
		if(!pageParameters.get("rows").isNull()){
			this.rows=pageParameters.get("rows").toInteger();
		}
		
		if(!pageParameters.get("skip").isNull()){
			this.skip=pageParameters.get("skip").toInteger();
		}
		
		if(!pageParameters.get("currentPage").isNull()){
			this.currentPage=this.pageParameters.get("currentPage").toInteger();
		}
		
		Integer records=0;
		boolean searchByType=false;
		if(pageParameters.get("product_sort_id").isNull()&&pageParameters.get("searchInfo").isNull()){
			throw new RuntimeException("url必须传递【product_sort_id】参数，商品分类。或者【searchInfo】，搜索信息");
		}else{
			if(!pageParameters.get("product_sort_id").isNull()){
				this.productSortId=pageParameters.get("product_sort_id").toInteger();
				records=this.gridBigPicService.countPageProductBySortId(this.productSortId);
				searchByType=true;
			}else{
				searchInfo=this.pageParameters.get("searchInfo").toString();
				records=this.gridBigPicService.countPageProductsBySearchInfo(searchInfo);
			}
		}
		
		if(Math.abs(skip)==2){
			switch(skip){
			
			case -2://首页
				this.currentPage=1;
				break;
			case 2://末页
				this.currentPage=(records+rows-1)/rows;
				break;
			}
		}else{
			this.currentPage+=skip;//-1,1
		}
		
		if(this.currentPage==1){
			this.firstBtnEnable=false;
			this.lastBtnEnable=false;
		}
		if(this.currentPage==(records+rows-1)/rows){
			this.nextBtnEnable=false;
			this.finalBtnEnabel=false;
		}

		final List<Product> products;
		if(searchByType){
			products=this.gridBigPicService.getPageProductBySortId(productSortId, currentPage, rows, records);
		}else{
			products=this.gridBigPicService.getPageProductsBySearchInfo(searchInfo, currentPage, rows, records);
		}
		
		AjaxLazyLoadPanel panel=new AjaxLazyLoadPanel("wk_product_grid") {
			
			@Override
			public Component getLazyLoadComponent(String arg0) {
				// TODO Auto-generated method stub
				return new GridBigPicPanel(arg0, "结果", products, SingleProductPage.class);
			}
		};
		add(panel);
	}
	
}
